package com.diaohw.module.metadata.service.dictionary;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diaohw.module.metadata.mapper.DictionaryMapper;
import com.diaohw.module.metadata.obj.dto.DictionaryDTO;
import com.diaohw.module.metadata.obj.entity.DictionaryDO;
import com.diaohw.module.metadata.obj.query.DictionaryQuery;
import com.diaohw.module.metadata.obj.vo.DictionaryItemVO;
import com.diaohw.module.metadata.obj.vo.DictionaryVO;
import com.diaohw.module.metadata.service.DictionaryItemService;
import com.diaohw.module.metadata.service.DictionaryService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.framework.db.utils.DataCheckUtils;
import com.diaohw.platform.framework.db.utils.WrapperUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-09-18
 */
@Service
public class DictionaryImpl extends ServiceImpl<DictionaryMapper, DictionaryDO> implements DictionaryService {

    @Resource
    private DictionaryItemService itemService;


    @Override
    public DictionaryVO create(DictionaryDTO data) {
        DataCheckUtils.eq(DictionaryDO::getDictionaryName, data.getDictionaryName(), this.baseMapper);
        DataCheckUtils.eq(DictionaryDO::getDictionaryCode, data.getDictionaryCode(), this.baseMapper);

        DictionaryDO model = INSTANCE.dtoToEntity(data);
        this.save(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public DictionaryVO update(String id, DictionaryDTO data) {
        DataCheckUtils.eq(DictionaryDO::getDictionaryName, id, data.getDictionaryName(), this.baseMapper);
        DataCheckUtils.eq(DictionaryDO::getDictionaryCode, id, data.getDictionaryCode(), this.baseMapper);
        DictionaryDO model = INSTANCE.dtoToEntity(data);
        model.setId(id);
        this.updateById(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        itemService.deleteByAttributeId(id);
        return this.removeById(id);
    }

    @Override
    public DictionaryVO detail(String id) {
        DictionaryDO model = this.getById(id);
        List<DictionaryItemVO> itemList = itemService.findByDictionaryId(Collections.singletonList(id));
        DictionaryVO record = INSTANCE.entityToVo(model);
        record.setChildren(itemList);
        return record;
    }

    @Override
    public List<DictionaryVO> findByList(DictionaryQuery query) {
        LambdaQueryWrapper<DictionaryDO> wrapper = createWrapper(query);
        List<DictionaryDO> list = this.list(wrapper);
        return selectChildren(list, query.getChildren());
    }

    @Override
    public PageResultVo<DictionaryVO> findByPage(DictionaryQuery query) {
        LambdaQueryWrapper<DictionaryDO> wrapper = createWrapper(query);
        Page<DictionaryDO> page = this.page(WrapperUtils.createPage(query), wrapper);
        return PageResultVo.create(page.getTotal(), selectChildren(page.getRecords(), query.getChildren()));
    }

    /**
     * 生成查询条件
     *
     * @param query 查询条件
     */
    private LambdaQueryWrapper<DictionaryDO> createWrapper(DictionaryQuery query) {
        LambdaQueryWrapper<DictionaryDO> wrapper = Wrappers.lambdaQuery(DictionaryDO.class);
        wrapper.like(StrUtil.isNotBlank(query.getDictionaryName()), DictionaryDO::getDictionaryName, query.getDictionaryName());
        wrapper.like(StrUtil.isNotBlank(query.getDictionaryCode()), DictionaryDO::getDictionaryCode, query.getDictionaryCode());
        wrapper.like(StrUtil.isNotBlank(query.getDescription()), DictionaryDO::getDescription, query.getDescription());
        wrapper.eq(ObjUtil.isNotNull(query.getDisplay()), DictionaryDO::getDisplay, query.getDisplay());
        wrapper.like(StrUtil.isNotBlank(query.getRemark()), DictionaryDO::getRemark, query.getRemark());
        return wrapper;
    }


    /**
     * 添加数据子项
     */
    public List<DictionaryVO> selectChildren(List<DictionaryDO> list, Boolean children) {
        if (list.isEmpty()) {
            return List.of();
        }


        List<DictionaryVO> data = INSTANCE.entityToVo(list);
        if (Boolean.TRUE.equals(children)) {
            List<String> dictionaryIds = list.stream().map(DictionaryDO::getId).toList();
            Map<String, List<DictionaryItemVO>> map = itemService.findByDictionaryId(dictionaryIds).stream()
                    .collect(Collectors.groupingBy(DictionaryItemVO::getDictionaryId));
            data.forEach(d -> d.setChildren(map.get(d.getId())));
        }
        return data;
    }


}
